package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysDrug;
import com.mars.module.admin.mapper.SysDrugMapper;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysDrugBorrowRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysDrugBorrowMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysDrugBorrow;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysDrugBorrowService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 药品租借记录业务层处理
 *
 * @author mars
 * @date 2024-05-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDrugBorrowServiceImpl implements ISysDrugBorrowService {

    private final SysDrugBorrowMapper baseMapper;
    private final SysDrugMapper drugMapper;
    private final SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDrugBorrow add(SysDrugBorrowRequest request) {
        SysDrugBorrow entity = SysDrugBorrow.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long id = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(id);
        entity.setUserId(id);
        entity.setIsReturn("0");//未归还
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysDrugBorrowRequest request) {
        SysDrugBorrow entity = SysDrugBorrow.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysDrugBorrow getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysDrugBorrow> pageList(SysDrugBorrowRequest request) {
        Page<SysDrugBorrow> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysDrugBorrow> query = this.buildWrapper(request);
        IPage<SysDrugBorrow> pageRecord = baseMapper.selectPage(page, query);
        List<SysDrugBorrow> records = pageRecord.getRecords();
        List<SysDrugBorrow> recordsNew = new ArrayList<>();
        for (SysDrugBorrow borrow : records) {
            SysUser sysUser = userMapper.selectById(borrow.getUserId());
            borrow.setUserName(sysUser == null ? "" : sysUser.getUserName());
            SysDrug drug = drugMapper.selectById(borrow.getDrugId());
            borrow.setDrugName(drug == null ? "" : drug.getName());
            if ("0".equals(borrow.getIsReturn())) {
                borrow.setIsReturn("未归还");
            } else if ("1".equals(borrow.getIsReturn())) {
                borrow.setIsReturn("已归还");
            }
            recordsNew.add(borrow);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysDrugBorrow> list(SysDrugBorrowRequest request) {
        LambdaQueryWrapper<SysDrugBorrow> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysDrugBorrow> buildWrapper(SysDrugBorrowRequest param) {
        LambdaQueryWrapper<SysDrugBorrow> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getBorrowDuration())) {
            query.like(SysDrugBorrow::getBorrowDuration, param.getBorrowDuration());
        }
        if (StringUtils.isNotBlank(param.getIsReturn())) {
            query.like(SysDrugBorrow::getIsReturn, param.getIsReturn());
        }
        if (param.getUserId() != null) {
            query.eq(SysDrugBorrow::getUserId, param.getUserId());
        }
        return query;
    }

}
